package com.bistu.sim.xwy.news.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bistu.sim.xwy.news.domain.Article;
import com.bistu.sim.xwy.news.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

public class ArticleAdapter extends BaseAdapter {
    private Context context;
    private List<Article> list;

    public ArticleAdapter(Context context, List<Article> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;

        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.art_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.art_image = view.findViewById(R.id.art_image);
            viewHolder.art_title = view.findViewById(R.id.art_title);
            viewHolder.describe_text = view.findViewById(R.id.describe_text);
            viewHolder.art_categroy = view.findViewById(R.id.art_categroy);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        Article item = list.get(position);
        viewHolder.art_title.setText(item.getTitle());
        viewHolder.describe_text.setText(item.getContent());
        viewHolder.art_categroy.setText(item.getCategories());

        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.drawable.load);

        String imageUrl = context.getString(R.string.article_url) + item.getThumbnail();
        Log.e("ArticleAdapter",imageUrl);
        Glide.with(context).load(imageUrl)
                .apply(requestOptions)
                .into(viewHolder.art_image);

        return view;
    }

    static class ViewHolder {
        ImageView art_image;
        TextView art_title;
        TextView describe_text;
        TextView art_categroy;
    }
}